<template>
	<view>
		<u-popup width="600rpx" border-radius="15" v-model="bindPhoneshow" mode="center">
			<view style="font-size: 36rpx;font-weight: bolder;text-align: center;width: 600rpx;margin-top: 40rpx;">温馨提示</view>
			<view style="width: 600rpx;height: 0.5rpx;background-color: #BDC3C7;margin-top: 20rpx;"></view>
			<view class="jh_u-f-ac" style="width: 600rpx;height: 250rpx;">
				<view style="font-size: 40rpx;font-weight: 700;text-align: center;margin-left: 40rpx;">是否使用手机号登录？</view>
			</view>
			<view style="display: flex;flex-direction: row;align-items: center;flex: 1;">
				<button style="width: 50%;height: 100rpx;" @click="bindPhoneshow = false">取消</button>
				<button style="width: 50%;height: 100rpx;" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">确定</button>
			</view>
		</u-popup>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	var _self;
	export default {
		name:"jh-authorize",
		data() {
			return {
				jsCode:'',
				bindPhoneshow:false,
				encryptedData:'',
				iv:'',
				openid:'',
				avatarUrl:''
			};
		},
		mounted() {
			_self = this;
			_self.init();
			
		},
		methods:{
			//---------------------------------------------快捷弹窗---------------------------------------------
			showToast: function(title, type) {
				_self.$refs.uToast.show({
					title: title,
					type: type
				})
			},
			//-------------------------------------------------初始化,获取code-------------------------------------------------
			async init(){
				console.log('0123');
				
				uni.$on('getAuthorize',function(data){
					console.log('0000',data);
					_self.wxAuthorize();
					// uni.$off('getAuthorize');
				});
				wx.login({
				    success: (res) => {
				        if (res.code) {
							console.log('组件获取code',res.code);
				            _self.jsCode=res.code;
				        } else {
				            console.log('登录失败！' + res.errMsg)
				        }
				    }
				})
				uni.getStorage({
					key: 'userInfo',
					success: function(res) {
						
					},
					fail: function() {
						_self.wxAuthorize();
					}
				})
			},
			//-------------------------------------------------授权-------------------------------------------------
			wxAuthorize:function(){
				uni.showModal({
					title: '温馨提示',
					content: '亲，授权微信登录后才能正常使用小程序功能',
					success(res) {
						//如果用户点击了确定按钮
						if (res.confirm) {
							_self.GetUserInfoByJsCode('微信用户');
							//第一步：调用uni.getUserProfile方法可以直接弹出授权框
							// uni.getUserProfile({
							// 	desc: '获取您的昵称、头像、地区及性别',
							// 	success: res => {
							// 		console.log(res);
							// 		_self.avatarUrl = res.userInfo.avatarUrl;
							// 		_self.GetUserInfoByJsCode(res.userInfo.nickName);
							// 	},
							// 	fail: res => {
							// 		console.log(res)
							// 		//拒绝授权
							// 		_self.showToast('您拒绝了请求,不能正常使用小程序', 'warning');
							// 		return
							// 	}
							// });
						} else if (res.cancel) {
							//如果用户点击了取消按钮
							_self.showToast('您拒绝了请求,不能正常使用小程序', 'warning');
							return
						}  
					}
				});
			},
			//获取openid
			GetUserInfoByJsCode:function(NickName){
				console.log(_self.$h.expressInterface.expressInterface.appSystemCode.appSystemCode,_self.jsCode,NickName);
				uni.request({
					url:_self.$h.expressInterface.expressInterface.GetUserInfoByJsCode.url,
					method:_self.$h.expressInterface.expressInterface.GetUserInfoByJsCode.method,
					header:_self.$h.expressInterface.expressInterface.GetUserInfoByJsCode.header,
					data:{
						appSystemCode:_self.$h.expressInterface.expressInterface.appSystemCode.appSystemCode,
						code:_self.jsCode,
						NickName:NickName
					},success:function(res){
						console.log('获取openid返回数据',res);
						if(res.data.Success){
							var arr = JSON.parse(res.data.Data);
							console.log(JSON.parse(res.data.Data));
							//授权成功，保存用户信息
							_self.showToast('授权成功', 'success');
							_self.loginState = true;
							var userArr = {
								Id:arr.AID,
								NickName:arr.NickName,
								OpenId:_self.openid,
								PhoneNumber:arr.PhoneNumber,
								SystemName:arr.SystemName,
								Name:arr.NickName,
								avatarUrl:_self.avatarUrl
							}
							_self.userInfo = userArr;
							console.log(_self.userInfo);
							uni.setStorageSync('userInfo', _self.userInfo);
							uni.setStorageSync('isAuthorize', true);
							_self.openid = arr.OpenID;
							_self.session_key = arr.Session_key;
							uni.setStorageSync('WXOpenID',arr.OpenID);
							_self.changeAuthorize(true);
							if(!arr.PhoneNumber || !_self.userInfo.PhoneNumber){
								_self.bindPhoneshow = true;
							}
						}else {
							uni.setStorageSync('isAuthorize', false);
							_self.showToast('授权失败', 'warning');
							_self.changeAuthorize(false);
						}
					},fail:function(res){
						_self.changeAuthorize(false);
						uni.setStorageSync('isAuthorize', false);
						console.log('失败返回',res);
						_self.$u.toast('获取openid失败');
					}
				})
			},
			//授权状态改变时发送通知
			changeAuthorize:function(state){
				uni.$emit('changeAuthorize', {
				    data: state
				});
			},
			//-------------------------------------------------弹出手机绑定框-------------------------------------------------
			getPhoneNumber:function(e){
				// 当用户点击并同意之后，可以通过 @getphonenumber 事件回调获取到微信服务器返回的加密数据
				//然后在第三方服务端结合 session_key 以及 app_id 进行解密获取手机号
				_self.bindPhoneshow = false;
				
				console.log(e.detail.errMsg)
				console.log(e.detail.iv)
				console.log(e.detail.encryptedData)
				_self.encryptedData = e.detail.encryptedData;
				_self.iv = e.detail.iv;
				if(e.detail.errMsg=="getPhoneNumber:fail user deny"){
					//用户决绝授权
				    _self.showToast('您拒绝了手机号登录！','warning');
				}else{
					//将iv、encryptedData传到后台，请求接口获取手机号
					_self.GetUserPhoneByJsKey();
				}
			},
			//-------------------------------------------------获取手机号-------------------------------------------------
			GetUserPhoneByJsKey:function(){
				uni.request({
					url:_self.$h.expressInterface.expressInterface.GetUserPhoneByJsKey.url,
					method:_self.$h.expressInterface.expressInterface.GetUserPhoneByJsKey.method,
					header:_self.$h.expressInterface.expressInterface.GetUserPhoneByJsKey.header,
					data:{
						appSystemCode:_self.$h.expressInterface.expressInterface.appSystemCode.appSystemCode,
						openid:_self.openid,
						session_key:_self.session_key,
						encryptedData:_self.encryptedData,
						iv:_self.iv
					},success:function(res){
						console.log('成功返回',res);
						if(res.data.Success){
							var arr = JSON.parse(res.data.Data);
							console.log(JSON.parse(res.data.Data));
							uni.setStorageSync('phoneNumber', res.PhoneNumber);
							_self.showToast('绑定手机成功', 'success');
						}else {
							_self.showToast('绑定手机失败','warning');
						}
					},fail:function(res){
						console.log('失败返回',res);
						_self.$u.toast('绑定失败');
					}
				})
			},
		}
	}
</script>

<style>

</style>
